package middleWare

import (
	"apitestAfterGo/common"
	"apitestAfterGo/utils"
	"github.com/golang-jwt/jwt"
	"github.com/kataras/iris/v12/context"
)

func JwtMiddleWare() context.Handler {
	return func(ctx *context.Context) {
		url := ctx.Request().URL.Path
		if url != "/login" && url != "/register" && url != "/password" {
			header := ctx.GetHeader("Authorization")
			token, err := jwt.Parse(header, func(token *jwt.Token) (interface{}, error) {
				return []byte(common.JwtKey), nil
			})
			if err != nil || !token.Valid {
				ctx.Application().Logger().Infof("请求路径:%s,错误原因:登录超时或者没有权限！", ctx.Path())
				ctx.JSON(utils.Error(common.GetResult(6).Code, common.GetResult(6).Msg))
				return
			}
		}
		ctx.Next()
	}
}
